package com.bwda.dsrs.systemservice.controller;


import com.bwda.dsrs.base.centerbase.response.CentreCutPageResponse;
import com.bwda.dsrs.base.centerbase.response.CentreListResponse;
import com.bwda.dsrs.base.centerbase.response.ResponseEntity;
import com.bwda.dsrs.base.util.CopyUtil;
import com.bwda.dsrs.systemservice.annotations.NeedRole;
import com.bwda.dsrs.systemservice.aspect.LogTypeEnum;
import com.bwda.dsrs.systemservice.aspect.Loggable;
import com.bwda.dsrs.systemservice.domain.condition.menuright.MenuRightCondition;
import com.bwda.dsrs.systemservice.domain.exception.SystemServiceException;
import com.bwda.dsrs.systemservice.domain.form.menuright.MenuRightCreateForm;
import com.bwda.dsrs.systemservice.domain.form.menuright.MenuRightQueryForm;
import com.bwda.dsrs.systemservice.domain.form.menuright.MenuRightUpdateForm;
import com.bwda.dsrs.systemservice.domain.po.menuright.MenuRightPo;
import com.bwda.dsrs.systemservice.domain.vo.menuright.MenuRightVo;
import com.bwda.dsrs.systemservice.service.MenuRightService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yangjun
 */
@RestController
@RequestMapping("/s/menuright")
@Api(value = "/s/menuright", tags = "菜单权限表")
public class MenuRightController extends BaseController {

    @Autowired
    private MenuRightService menuRightService;

    @ApiOperation(value = "查询菜单权限表", notes = "查询菜单权限表", httpMethod = "GET")
    @Loggable(descp = "查询菜单权限表",type = LogTypeEnum.QUERY,include = "")
    @RequestMapping(value = "/query", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<MenuRightVo> query(@ApiParam(value = "菜单ID", required = true) @RequestParam Integer id) throws SystemServiceException {
        MenuRightPo po = menuRightService.queryWithValid(id);
        MenuRightVo vo = CopyUtil.transfer(po, MenuRightVo.class);
        return getSuccessResult(vo);
    }

    @ApiOperation(value = "查询菜单权限表数量", notes = "查询菜单权限表数量", httpMethod = "GET")
    @Loggable(descp = "查询菜单权限表数量",type = LogTypeEnum.QUERY,include = "")
    @RequestMapping(value = "/queryCount", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<Integer> queryCount(@ModelAttribute @Valid MenuRightQueryForm form) throws SystemServiceException {
        MenuRightCondition condition = this.getConditionByQueryForm(form);
        int count = menuRightService.queryCount(condition);
        return getSuccessResult(count);
    }

    @ApiOperation(value = "查询菜单权限表列表", notes = "查询菜单权限表列表", httpMethod = "GET")
    @Loggable(descp = "查询菜单权限表列表",type = LogTypeEnum.QUERY,include = "")
    @RequestMapping(value = "/queryList", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<CentreListResponse<MenuRightVo>> queryList(@ModelAttribute @Valid MenuRightQueryForm form) throws SystemServiceException {
        MenuRightCondition condition = this.getConditionByQueryForm(form);
        List<MenuRightPo> poList = menuRightService.queryList(condition);
        List<MenuRightVo> voList = CopyUtil.transfer(poList, MenuRightVo.class);
        return getSuccessResult(getListResponse(voList));
    }

    @ApiOperation(value = "查询菜单权限表列表(带分页)", notes = "查询菜单权限表列表(带分页)", httpMethod = "GET")
    @Loggable(descp = "查询菜单权限表列表(带分页)",type = LogTypeEnum.QUERY,include = "")
    @RequestMapping(value = "/queryPageList", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<CentreCutPageResponse<MenuRightVo>> queryPageList(@ModelAttribute @Valid MenuRightQueryForm form) throws SystemServiceException {
        MenuRightCondition condition = this.getConditionByQueryForm(form);
        List<MenuRightVo> voList = new ArrayList<>();
        int count = menuRightService.queryCount(condition);
        if (count > 0) {
            List<MenuRightPo> poList = menuRightService.queryList(condition);
            voList = CopyUtil.transfer(poList, MenuRightVo.class);
        }
        return getSuccessResult(getPageResponse(form, count, voList));
    }
    @NeedRole("sysadminrole")
    @ApiOperation(value = "新增菜单权限表", notes = "新增菜单权限表", httpMethod = "POST")
    @Loggable(descp = "新增菜单权限表",type = LogTypeEnum.CREATE,include = "")
    @RequestMapping(value = "/add", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<MenuRightVo> add(@ModelAttribute @Valid MenuRightCreateForm form) throws SystemServiceException {
        MenuRightPo po = CopyUtil.transfer(form, MenuRightPo.class);
        po.setId(1);
        menuRightService.insert(po);
        MenuRightVo vo = CopyUtil.transfer(po, MenuRightVo.class);
        return getSuccessResult(vo);
    }
    @NeedRole("sysadminrole")
    @ApiOperation(value = "修改菜单权限表", notes = "修改菜单权限表", httpMethod = "POST")
    @Loggable(descp = "修改菜单权限表",type = LogTypeEnum.UPDATE,include = "")
    @RequestMapping(value = "/update", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity update(@ModelAttribute @Valid MenuRightUpdateForm form) throws SystemServiceException {
        MenuRightPo po = CopyUtil.transfer(form, MenuRightPo.class);
        menuRightService.update(po);
        return getSuccessResult();
    }
    @NeedRole("sysadminrole")
    @ApiOperation(value = "删除菜单权限表", notes = "删除菜单权限表", httpMethod = "POST")
    @Loggable(descp = "删除菜单权限表",type = LogTypeEnum.DELETE,include = "")
    @RequestMapping(value = "/delete", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity delete(@ApiParam(value = "菜单ID", required = true) @RequestParam Integer id) throws SystemServiceException {
        menuRightService.delete(id);
        return getSuccessResult();
    }

    /**
     * MenuRightQueryForm转换为MenuRightCondition
     *
     * @param form
     * @return
     */
    private MenuRightCondition getConditionByQueryForm(MenuRightQueryForm form) {
        MenuRightCondition condition = CopyUtil.transfer(form, MenuRightCondition.class);
        return condition;
    }

}